<?php

/*
 * This class is used for as Model for Building table 
 */

class Model_Building extends Model_Base {

    protected $_name = 'tbl_buildings';
    protected $_primaryKey = 'build_id';

    //get site data
    public function getsiteArray() {
        $siteModel = new Model_Site();
        $siteArray = $siteModel->fetchSite();
        $finalArray[''] = '';
        foreach ($siteArray as $com) {
            $finalArray[$com['site_id']] = $com['site_name'];
        }
        return $finalArray;
    }

    //get country data
    public function getcountryArray() {
        $countryModel = new Model_Country();
        $countryArray = $countryModel->fetchall();
        $finalArray[''] = '-- Select Country --';
        foreach ($countryArray as $com) {
            $finalArray[$com['country_id']] = $com['country_name'];
        }
        return $finalArray;
    }

    /**
     * Function to return different status of the Building
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getStatusArray() {
        return array(
            #'' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    /**
     * Fetch an individual entry for view
     * 
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryByViewId($id) {
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('building' => 'tbl_buildings'))
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = building.updated_lgn_id")
                ->join(array('site' => 'tbl_site'), "site.site_id = building.site_id")
                ->where('build_id = ?', $id)
                ->where('build_status != 2');
        #echo $select;exit;
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('building' => 'tbl_buildings'))
                ->setIntegrityCheck(false)
                ->where('build_id = ?', $id)
                ->where('build_status != 2');
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get buildings
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchBuilding($where = "", $limit = "", $offset = "0", $order = 'build_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('building' => 'tbl_buildings'))
                ->setIntegrityCheck(false)
                ->join(array('site' => 'tbl_site'), "site.site_id = building.site_id")
                //->join(array('com' => 'tbl_company'), "com.com_id = site.com_id")
                ->where('build_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total rows
     * @return int
     */
    public function getBuildingCount($where) {
        $select = $this->select()
                ->from(array('building' => 'tbl_buildings'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('site' => 'tbl_site'), "site.site_id = building.site_id")
                ->where('build_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete buildings
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteBuilding($prdtArray) {

        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_building) {
                $buildingData = Array();
                $buildingData = $this->fetchEntryById($_building);
                if (count($buildingData) > 0) {
                    $data = array('build_status' => '2');
                    $this->update($data, 'build_id =' . (int) $_building);
                }
            }
        } else {
            $build_id = $prdtArray;
            $buildingData = $this->fetchEntryById($build_id);
            if (count($buildingData) > 0) {
                $data = array('build_status' => '2');
                $this->update($data, 'build_id =' . $build_id);
            }
        }
    }

}
